package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.entity.ClassifyEntity;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.AutoSortAdapter;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bao on 2017/5/3.
 */
public class AutoSortPopup extends BasePopupWindow {

    private View popupView;
    private RecyclerView rl_classify;
    private AutoSortAdapter mAutoSortAdapter;
    private OnClickItemListener mOnClicktemListener;

    private List<ClassifyEntity> list;
    private ClassifyEntity mClassifyEntity;


    public AutoSortPopup(Activity context) {
        super(context);
        bindEvent();
        initAdapter();
    }

    @Override
    public Animation getAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_find_popup, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            rl_classify = (RecyclerView) popupView.findViewById(R.id.rl_classify);
        }
    }

    private void initAdapter() {
        initData();
        mAutoSortAdapter = new AutoSortAdapter();
        rl_classify.setLayoutManager(new LinearLayoutManager(mContext));
        mAutoSortAdapter.setNewData(list);
        rl_classify.setAdapter(mAutoSortAdapter);
        mAutoSortAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mClassifyEntity = (ClassifyEntity) adapter.getData().get(position);
                mOnClicktemListener.onClickItem(mClassifyEntity);
                dismiss();
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        ClassifyEntity all = new ClassifyEntity();
        all.setClassifyName("距离");
        all.setId("1");
        list.add(all);
        ClassifyEntity sangna = new ClassifyEntity();
        sangna.setClassifyName("评分");
        sangna.setId("2");
        list.add(sangna);
        ClassifyEntity ktv = new ClassifyEntity();
        ktv.setClassifyName("价格");
        ktv.setId("3");
        list.add(ktv);
        ClassifyEntity ye = new ClassifyEntity();
        ye.setClassifyName("智能");
        ye.setId("4");
        list.add(ye);
        ClassifyEntity bar = new ClassifyEntity();
        bar.setClassifyName("人气");
        bar.setId("5");
        list.add(bar);
        SPUtil.putAutoSortClass(list.get(0));
    }

    public interface OnClickItemListener {
        void onClickItem(ClassifyEntity classifyEntity);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.mOnClicktemListener = onClickItemListener;
    }

    public void selectItem(ClassifyEntity classifyEntity){
        SPUtil.putAutoSortClass(classifyEntity);
        mAutoSortAdapter.selAutoSort(classifyEntity);
    }
}
